/*******************************************************************************
 * 版权：北京润尼尔网络科技有限公司，保留所有版权<br>
 * 版本：v1.0.0<br>
 * 文件：BaseDialog.js<br>
 * 日期：2017年7月18日<br>
 * 作者: 杨顺强<br>
 * 功能：实验结果分析对话框基类<br>
 * 
 * 修改记录：<br>
 * 日期 描述 更新功能<br>
 * 
 ******************************************************************************/
window.makeNamespace(window,"Rainier.UI.V2");
Rainier.UI.V2.InteractiveAnalysisDialog = Rainier.UI.V2.AnalysisBaseDialog.extend({
	
	NAME : "Rainier.UI.V2.InteractiveAnalysisDialog",

	/**
	 * @名称   init
	 * @功能   初始化dialog组件对象
	 * @author 杨顺强
	 * @data   2017-07-18
	 * @return {[type]}   [description]
	 */
	init : function(){
		var _this = this;
		var dialogProperties = {
		    title: '交互式仿真分析',
		    width: 492,
		    height: 404,
		    closed: false,
		    modal: true,
		    buttons:[{
					text:'运行',
					id : 'cfg-run',
					handler:function(){
						_this.onClickConfirm();
					}
				},{
					text:'保存',
					id : 'cfg-save',
					handler:function(){
						_this.onClickSave();
					}
				},{
					text:'取消',
					id : 'cfg-cancle',
					handler:function(){
						_this.onClickCancel();
					}
				}]
		};

		this.analysisParamsTab      = null;
		this.userDefineTab          = null;
		this.analysisTabComponent   = {};
		this.userDefineTabComponent = {};
	 	this._super(dialogProperties);	
	},

	/**
	 * @名称   createDialogComponents
	 * @功能   创建dialog组件组向的子组件对象
	 * @author 杨顺强
	 * @data   2017-07-18
	 * @return {[type]}   [description]
	 */
	createDialogComponents : function(){
		var tabSupport 		   = new Rainier.UI.V2.UICompositeTabSupport(this.dialog,{fit:true});

		var tabProperties1 	   = {title:'分析参数',closable:false,border:true,resizeable:true,fit:true};;
		this.analysisParamsTab = new Rainier.UI.V2.UICompositeTab(tabSupport,tabProperties1);

		var tabProperties2 	   = {title:'用户自定义分析选项',closable:false,border:true,resizeable:true,fit:true};
		this.userDefineTab 	   = new Rainier.UI.V2.UICompositeUserDefineTab(tabSupport,tabProperties2);

		tabSupport.select("分析参数");
		this.createAnalysisTabChildComponents();

		this.userDefineTabComponent = this.userDefineTab.getComponentsMap();

		this.registerComponent(this.analysisTabComponent);
		this.registerComponent(this.userDefineTabComponent);

		var step = this.circuitParser.computeMinAndMaxStep();
		this.componentsMap["#3"].setComponentValue(step.maxStep);
		this.componentsMap["#4"].setComponentValue(step.minStep);

		this.initValuesMap = this.getComponentsValues();

		if(this.experimentConfig){				
			for(var key in this.experimentConfig){
				this.componentsMap[key].setComponentValue(this.experimentConfig[key]);
				if(this.experimentConfig[key] !== this.initValuesMap[key]){
					if(this.componentsMap[key].getCheckBox){
						var checkboxComponent = this.componentsMap[key].getCheckBox();
						var checkbox = checkboxComponent.getCheckBox();
						$(checkbox).click();
					}
				}
			}
		}
	},

	/**
	 * @名称   createAnalysisTabChildComponents
	 * @功能   完善analysisParamsTab 组件的布局
	 * @author 杨顺强
	 * @data   2017-07-18
	 * @return {[type]}   [description]
	 */
	createAnalysisTabChildComponents : function(){
		var _this = this;
		var initValuePro  = {
			optionName : "#1",
			checkbox   :true,
			text	   :{
				style:"margin-left:10px;margin-top:5px;float:left",
				text :"使用储能元件的初始化条件",
			},
			inputValue:"uic"
		}
		var EnergyStoragecomponent = new Rainier.UI.V2.UICompositeCheckBoxTextHiddenInput(this.analysisParamsTab,initValuePro);
		this.analysisTabComponent = $.extend(this.analysisTabComponent,{"#1":EnergyStoragecomponent});
		
		var stopTimeProperties = {
			optionName : "#2",
			text	   :{
				style:"margin-left:30px;margin-top:5px;width:150px;float:left",
				text :"结束时间(TSTOP):",
			},
			textbox	   :{width:150,heigth:25,value:"1e+030",validType:'checkFloat'},
			unit	   :"s"
		};
		
		var StopTimeComponent = new Rainier.UI.V2.UICompositeTextTextBoxOrComboBoxUnit(this.analysisParamsTab,stopTimeProperties);
		this.analysisTabComponent = $.extend(this.analysisTabComponent,{"#2":StopTimeComponent});
		
		var maxTimeStepProperties = {
			optionName :"#3",
			checkbox   :true,
			compositeComponent:{
				optionName :"#3",
				text	   : {
					style:"margin-left:10px;margin-top:5px;width:150px;float:left",
					text :"最大时间步长(TMAX):"
				},
				textbox    : {
					width:150,
					height:25,
					value :"0.001",
					validType:'checkFloat'
				},
				unit:"s"
			}
		};
		var MaxTimeStepComponent = new Rainier.UI.V2.UICompositeCheckBoxTextTextBoxUnit(this.analysisParamsTab,maxTimeStepProperties);
		this.analysisTabComponent = $.extend(this.analysisTabComponent,{"#3":MaxTimeStepComponent});
		
		var textPro = {
			style:"margin-left:40px;margin-top:10px;width:300px;float:left",
			text :"设置较小的TMAX提高精度的同时会增加仿真时间"
		}
		var text = new Rainier.UI.V2.UICompositeText(this.analysisParamsTab,textPro);
		
		var beginTimeStepProperies = {
			optionName :"#4",
			checkbox   :true,
			compositeComponent:{
				optionName :"#4",
				text	   : {
					style:"margin-left:10px;margin-top:5px;width:150px;float:left",
					text :"初始时间步长(TSTEP):"
				},
				textbox    : {
					width:150,
					height:25,
					value :"0.0001",
					validType:'checkFloat'
				},
				unit:"s"
			}
		};
		var BeginTimeStepComponent = new Rainier.UI.V2.UICompositeCheckBoxTextTextBoxUnit(this.analysisParamsTab,beginTimeStepProperies);
		this.analysisTabComponent = $.extend(this.analysisTabComponent,{"#4":BeginTimeStepComponent});
		
		var buttonPro = {
				style : "margin-left:340px;margin-top:70px;float:left;",
				button:{width:90,heigth:25,text:"恢复到默认值"}
		}
		var decrationButton = new Rainier.UI.V2.UICompositeDecorationButton(this.analysisParamsTab,buttonPro);
		this.restoreButton = decrationButton.getButton();

		var RestoreButtonListener = Rainier.UI.V2.UIEventListener.extend({
			NAME : "RestoreButtonListener",
			
			init : function(){
		            this._super();                  
	        },
		        
			onEvent : function(sEvent,objEventSource,objArgs){
				_this.restoreDefalutValue();
				for(var option in _this.analysisTabComponent){
					if(_this.analysisTabComponent[option].getCheckBox){
						_this.analysisTabComponent[option].getCheckBox().setValue(false);
						_this.analysisTabComponent[option].checkBoxValueChanged();
					}
				}
			}
		});
		this.restoreButton.registerEventListener("onClick",new RestoreButtonListener());

	},

	/**
	 * @名称   makeSpice
	 * @功能   计算并产生spice语句，用于计算引擎进行计算
	 * @author 杨顺强
	 * @data   2017-07-18
	 * @return {[type]}   [description]
	 */
	makeSpice : function(){
		var objValues 	  =  this.getComponentsValues();
		var spiceSentence =  this.userDefineTab.makeSpice();
		spiceSentence += "\n.tran ";
		var arr1 = ["#4","#2","#3","#1"];
		$.each(arr1,function(index,key){
			if(objValues[key]){
				spiceSentence += objValues[key] + ' ';
				if(key === '#2'){
					spiceSentence += '0 ';
				}
			}
		});
		return spiceSentence;
	},

	/**
	 * @名称   restoreDefalutValue
	 * @功能   重置分析面板上的值为默认值
	 * @author 杨顺强
	 * @data   2017-07-18
	 * @return {[type]}   [description]
	 */
	restoreDefalutValue : function(){
		for(var optionName in this.analysisTabComponent){
			if(this.analysisTabComponent[optionName].setComponentValue){						
				this.analysisTabComponent[optionName].
				setComponentValue(this.initValuesMap[optionName]);
			}
		}
	},

	/**
	 * @名称   validateStopTime
	 * @功能   校验结束时间设置是否正确，结束时间必须大于最大时间步长
	 * @author 杨顺强
	 * @data   2017-07-18
	 * @return {[type]}   [description]
	 */
	validateStopTime : function(){
		var _this  = this;
		var stopTimeComponent = this.componentsMap["#2"];
		var maxStepComponent  = this.componentsMap["#3"];
		var stopValue         = stopTimeComponent.getTextBox().getValue();
		var maxStepValue      = maxStepComponent.getTextBox().getValue();
		if(parseFloat(stopValue) < parseFloat(maxStepValue)){
			this.analysisParamsTab.getParent().select(0);
			return false;
		}
		return true;
	},
	
	/**
	 * @名称   validateTStep
	 * @功能   校验步长设置是否正确，结束时间步长必须大于开始时间步长
	 * @author 杨顺强
	 * @data   2017-07-18
	 * @return {[type]}   [description]
	 */
	validateTStep : function(){
		var minStepComponent = this.componentsMap["#4"];
		var maxStepComponent = this.componentsMap["#3"];
		var minTextBox 		 = minStepComponent.getTextBox();
		var maxTextBox 		 = maxStepComponent.getTextBox();
		var minTextBoxValue  = minTextBox.getValue();
		var maxTextBoxValue  = maxTextBox.getValue();
		if(parseFloat(minTextBoxValue)>maxTextBoxValue){
			this.analysisParamsTab.getParent().select(0);
			return false;
		}
		return true;
	},
	
	
	/**
	 * @名称   validateComponent
	 * @功能   校验dialog组件内部所有组件值设置是否正确，
	 * @author 杨顺强
	 * @data   2017-07-18
	 * @return {[type]}   [description]
	 */
	validateComponent : function(){
		//禁用组件
		var flag = true;
		for(var component in this.componentsMap){
			if(this.componentsMap[component].isValid){
				this.componentsMap[component].getTextBox().enable();
				flag = this.componentsMap[component].isValid();
				if(!flag){
					if(this.componentsMap[component].getCheckBox){
						var checkboxComponent = this.componentsMap[component].getCheckBox();
						if(!checkboxComponent.getValue()){							
							$(checkboxComponent.getCheckBox()).click();
						}
					}
					var parentTab  = this.componentsMap[component].getParent();
					var tabTitle   = parentTab.getElement().panel('options').title;
					var tabSupport = parentTab.getParent();
					tabSupport.select(tabTitle);
					return flag;
				}
				if(this.componentsMap[component].getCheckBox){					
					this.componentsMap[component].getTextBox().disable();
				}
			}
		}
		return flag;
	},

	/**
	 * @名称   onClickConfirm
	 * @功能   点击运行按钮，实现实验的运行
	 * @author 杨顺强
	 * @data   2017-07-18
	 * @return {[type]}   [description]
	 */
	onClickConfirm : function(){
		var _this  = this;
		var flag1 = this.validateTStep();
		if(!flag1){
			var message = "最大时间步长的值必须大于等于初始时间步长的值，请进行相应的修改！！！";
			var messageDialog = _this.createMessageDialog(message);
			return false;
		}
		var flag2 = this.validateStopTime();
		if(!flag2){
			var message = "结束时间的值必须大于等于最大时间步长的值，请进行相应的修改！！！";
			var messageDialog = _this.createMessageDialog(message);
			return false;
		}
		var flag3 = _this.validateComponent();
		if(!flag3){
			var message = "请参照提示输入正确的参数值,方可保存！！！";
			var messageDialog = _this.createMessageDialog(message);
			return false;
		}
		var values = this.getComponentsValues();
		
		//todos 将界面上修改的参数配置进行存储，运行时添加的执行语句中
		//运行程序，执行程序的运行
		this.context.experimentConfig = values;
		// 启动
		window.app.getContext().runExperiment();
		window.app.getContext().controlAnalyse();
		
		this.dialog.close();
	},

	/**
	 * @名称   onClickSave
	 * @功能   点击保存按钮，完成配置参数信息的保存
	 * @author 杨顺强
	 * @data   2017-07-18
	 * @return {[type]}   [description]
	 */
	onClickSave : function(){
		var _this  = this;
		var flag1 = this.validateTStep();
		if(!flag1){
			var message = "最大时间步长的值必须大于等于初始时间步长的值，请进行相应的修改！！！";
			var messageDialog = _this.createMessageDialog(message);
			return false;
		}
		var flag2 = this.validateStopTime();
		if(!flag2){
			var message = "结束时间的值必须大于等于最大时间步长的值，请进行相应的修改！！！";
			var messageDialog = _this.createMessageDialog(message);
			return false;
		}
		var flag3 = _this.validateComponent();
		if(!flag3){
			var message = "请参照提示输入正确的参数值,方可保存！！！";
			var messageDialog = _this.createMessageDialog(message);
			return false;
		}
		var values = _this.getComponentsValues();
		
		//todos 将界面上修改的参数配置进行存储，运行时添加的执行语句中
		this.context.experimentConfig = values;

        window.app.getContext().controlAnalyse();
		this.dialog.close();
	},

	/**
	 * 函数名称：onClickCancel<br>
	 * 函数功能：点击了对话框的取消按钮，执行该操作<br>
	 * 参数输入：无<br>
	 * 参数输出：无<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	onClickCancel : function(){
		window.app.getContext().controlAnalyse();
		this.dialog.close();
	},

	/**
	 * @名称   createMessageDialog
	 * @功能   创建提示消息对话框
	 * @author 杨顺强
	 * @data   2017-07-18
	 * @return {[type]}   [description]
	 */
	createMessageDialog : function(message){
		var MessageDialog = Rainier.UI.V2.BaseDialog.extend({
			
			NAME : "MessageDialog",
			
			init : function(message){
				var _this = this;
				this.message = message;
				var dialogProperties = {
					    title: '错误信息：',
					    width: 310,
					    height: 140,
					    content:_this.message,
					    closed: false,
					    modal: true,
					    headerCls: "err-message-header",
					    bodyCls  : "err-message-body",
					    buttons:[{
							text:'关闭',
							height:22,
							handler:function(){
								_this.onClickConfirm();
							}
					    }]
				};
				this._super(dialogProperties);
			},
			
			createDialogComponents : function(parentcom){
				//nothing todo
			}
		});
		var messageInfo = String() + 
			"<div class='err-container'>" +
				"<div class='err-content'>&nbsp;&nbsp;"+
					"<div>&nbsp;&nbsp;"+message+"</div>"+
				"</div>"+
			"</div>";
		return new MessageDialog(messageInfo);
	},
});